<template>
  <el-dialog
    title="新增员工"
    :visible="dialogVisible"
    center
    @close="closeDialog"
    @open="openDialog"
  >
    <template #default>
      <el-form ref="ruleForm" :model="formData" :rules="rules" label-width="100px" class="demo-ruleForm">
        <el-form-item label="姓名" prop="username">
          <el-input v-model="formData.username" autocomplete="off" />
        </el-form-item>
        <el-form-item label="手机" prop="mobile">
          <el-input v-model="formData.mobile" autocomplete="off" />
        </el-form-item>
        <el-form-item label="入职时间" prop="timeOfEntry">
          <el-date-picker v-model="formData.timeOfEntry" style="width:50%" placeholder="请选择入职时间" />
        </el-form-item>
        <el-form-item label="聘用形式" prop="formOfEmployment">
          <el-select v-model="formData.formOfEmployment" style="width:50%" placeholder="请选择">
            <el-option
              v-for="item in hireList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="工号" prop="workNumber">
          <el-input v-model="formData.workNumber" />
        </el-form-item>
        <el-form-item label="部门" prop="departmentName">
          <el-cascader
            v-model="formData.departmentName"
            :options="departList"
            :props="defaultProps"
            clearable
          />
        </el-form-item>
        <el-form-item label="转正时间">
          <el-date-picker v-model="formData.correctionTime" style="width:50%" placeholder="请选择转正时间" />
        </el-form-item>
      </el-form>
    </template>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-button @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="confim">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { NewEmployeeAPI } from '@/api/empioyee'
import { fetchDepartmentList } from '@/api/department'
import transTree from '@/utils/transTree'
export default {
  props: {
    dialogVisible: {
      type: Boolean
    }
  },
  data() {
    return {
      // 员工数据项
      formData: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      hireList: [
        {
          id: 1,
          name: '正式'
        },
        {
          id: 2,
          name: '非正式'
        }
      ],
      departList: [], // 树形结构数据
      defaultProps: { checkStrictly: true, label: 'name', value: 'name' },
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: ['blur', 'change'] }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: ['blur', 'change'] },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: ['blur', 'change'] }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: ['blur', 'change'] }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: ['blur', 'change'] }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: ['blur', 'change'] }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  methods: {
    closeDialog() {
      this.$emit('close-dialog')
    },
    // 树状结构处理加调用
    async openDialog() {
      const res = await fetchDepartmentList()
      this.departList = transTree(res.depts)
    },
    // 点击发送请求
    confim() {
      this.$refs.ruleForm.validate(async valid => {
        if (valid) {
          // 调用新增接口
          await NewEmployeeAPI({
            ...this.formData,
            // 处理下数组数据
            departmentName: this.formData.departmentName[this.formData.departmentName.length - 1]
          })
          this.closeDialog()
        }
      })
    }

  }
}
</script>

<style>

</style>
